package com.itheima.service.impl;

import com.itheima.mapper.OperateLogMapper;
import com.itheima.pojo.OperateLog;
import com.itheima.pojo.PageBean;
import com.itheima.service.OperateLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 操作日志服务实现类
 */
@Slf4j
@Service
public class OperateLogServiceImpl implements OperateLogService {
    
    @Autowired
    private OperateLogMapper operateLogMapper;
    
    @Override
    public void insertLog(OperateLog operateLog) {
        operateLogMapper.insert(operateLog);
    }
    
    @Override
    public PageBean page(Integer page, Integer pageSize, Integer operateEmpId, 
                        LocalDateTime startDate, LocalDateTime endDate, 
                        String className, String methodName) {
        // 设置分页参数
        int start = (page - 1) * pageSize;
        
        // 查询总记录数和当前页数据
        long count = operateLogMapper.selectCount(operateEmpId, startDate, endDate, className, methodName);
        List<OperateLog> logList = operateLogMapper.selectPage(start, pageSize, operateEmpId, startDate, endDate, className, methodName);
        
        // 封装PageBean对象
        PageBean pageBean = new PageBean(count, logList);
        return pageBean;
    }
    
    @Override
    public void deleteById(Integer id) {
        operateLogMapper.deleteById(id);
    }
    
    @Override
    public void deleteByIds(List<Integer> ids) {
        operateLogMapper.deleteByIds(ids);
    }
    
    @Override
    public void deleteAll() {
        operateLogMapper.deleteAll();
    }
    
    @Override
    public Map<String, Object> getStatistics() {
        Map<String, Object> statistics = new HashMap<>();
        statistics.put("todayLogs", operateLogMapper.countToday());
        statistics.put("weekLogs", operateLogMapper.countThisWeek());
        statistics.put("monthLogs", operateLogMapper.countThisMonth());
        statistics.put("totalLogs", operateLogMapper.countTotal());
        return statistics;
    }
    
    @Override
    public List<Map<String, Object>> getTrendData() {
        return operateLogMapper.getTrendData();
    }
    
    @Override
    public List<Map<String, Object>> getTypeData() {
        return operateLogMapper.getTypeData();
    }
}
